package com.test.java.juc.thread;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 *  ReentrantLock 基本使用
 */
@Slf4j
public class ReentrantLockDemo {

    private static ReentrantLock lock = new ReentrantLock();

    public static void main(String[] args) {

        ExecutorService pool = Executors.newFixedThreadPool(3);

        for (int i = 0; i < 3; i++) {
            pool.submit(()->{
                testReentrantLock();
            });
        }

        Thread.yield();


    }

    public static void testReentrantLock(){

        // 获取锁
        lock.lock();
        log.info("当前线程{} 获取到锁", Thread.currentThread().getName());

        try {
            log.info("当前线程 {} 正在执行任务", Thread.currentThread().getName());
            Thread.sleep(2000L);
            log.info("当前线程 {} 执行完成", Thread.currentThread().getName());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
            log.info("当前线程{} 释放锁", Thread.currentThread().getName());
        }
    }

    public static void testReentrantLockCondition(){

        Condition condition = lock.newCondition();


    }
}
